package cn.dylan.order.service.impl;

import cn.dylan.basic.service.impl.BaseServiceImpl;
import cn.dylan.basic.util.PageList;
import cn.dylan.order.domain.OrderPetAcquisition;
import cn.dylan.order.dto.OrderDto;
import cn.dylan.order.mapper.OrderPetAcquisitionMapper;
import cn.dylan.order.query.OrderPetAcquisitionQuery;
import cn.dylan.order.service.IOrderPetAcquisiionService;
import cn.dylan.org.domain.Employee;
import cn.dylan.org.domain.Shop;
import cn.dylan.org.mapper.EmployeeMapper;
import cn.dylan.org.mapper.ShopMapper;
import cn.dylan.pet.domain.Pet;
import cn.dylan.pet.domain.SearchMasterMsg;
import cn.dylan.pet.mapper.PetMapper;
import cn.dylan.pet.mapper.SearchMasterMsgMapper;
import lombok.experimental.PackagePrivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderPetAcquisiionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisiionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPagePlus(OrderPetAcquisitionQuery query, Long id) {

        // 分页查询，根据权限查
            // 1. 管理员权限，emp  表中shop_id 为空，能看到所有订单shop_id 为空
                //2. 店铺管理员权限 shop表中的(admin_id = emp_id)  能看到店铺所有的订单
                    // 3. 普通店员权限 shop表中的admin_id 不等  给emp_id 赋值  仅能看到自己的订单
        // 1.先根据logininfoId查询emp
        Employee emp = employeeMapper.loadByLogininfoId(id);
        if(emp.getShop_id()!=null){// 非台管理员 需要给query中的shopId 赋值
            query.setShopId(emp.getShop_id());
            // 根据shopId 查shop 获取admin_id
            Shop shop = shopMapper.LoadById(emp.getShop_id());
            if(shop.getAdmin_id()!=emp.getId()){//不是店铺管理员，查到的数据受到empId 的限制
                query.setEmpId(emp.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto dto) {
        Long orderId = dto.getId();
        Integer petType = dto.getPayType();
        BigDecimal money = dto.getMoney();
        /**
         * 确定订单
         *      1.订单状态改成 已处理，price保存 payType保存
         *      2.对应的pet costprice 成本价  state 改成已处理
         *      3.search_master_msg 里面的状态该成已处理
         */
        // 先根据dto中的OderId查询出当前订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.LoadById(orderId);
        //该数据
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(petType);
        orderPetAcquisition.setState(2);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 该对应pet数据
        Pet pet = petMapper.loadByPetId(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);

        // 改search_master_msg
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.LoadById(pet.getSearch_master_msg_id());
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    @Override
    public void cancel(Long id) {
        // 取消订单
        /**
         * 取消订单  订单状态改成取消  -1
         * 删除pet  删除petDetail
         * searchMasterMsg shopId置空
         */
        // 置空
        searchMasterMsgMapper.reject(id);
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.LoadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        petMapper.delete(orderPetAcquisition.getPet_id());
    }
}
